package com.shop2cn.iapi.productsynces.service;

import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import com.ymatou.iapi.productsynces.parameter.es.EsProduct;
import com.ymttest.business.YmatouAutoTestCall;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;

public class ESsearchCall extends  YmatouAutoTestCall{
	private static String opurl ="productquery/_search";
	
	public ESsearchCall() {
		//super(EnvSetup.getSystemUrl("esquery"),opurl,"GET","JSON");
		super(EnvSetup.getSystemUrl("esquery_shop2cn"),opurl,"GET","PARAM");
	}
	@Override
	public void addDefHeader() {
		client.addHeader("Content-Type", "application/json; charset=UTF-8");
	}
	
	public EsProduct callandGetBean(String productId) throws Exception{
		JSONObject ret = new ESsearchCall().callAndGetReturnData(MapUtil.hashMap("q", "pid:"+productId));
		if (ret.getJSONObject("hits").getJSONArray("hits").length()==0){
			return null;
		}
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
		EsProduct esproduct = gson.fromJson(ret.getJSONObject("hits").getJSONArray("hits").getJSONObject(0)
				.getJSONObject("_source").toString(), EsProduct.class);
		
		
		esproduct.setIspackage(ret.getJSONObject("hits").getJSONArray("hits").getJSONObject(0)
		.getJSONObject("_source").getBoolean("package"));
		
		
		return esproduct;
	}
	
	
	public EsProduct getEsProduct() throws JsonSyntaxException, JSONException
	{
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
		EsProduct esproduct = gson.fromJson(new JSONObject(getReturnData()).getJSONObject("hits").getJSONArray("hits").getJSONObject(0)
				.getJSONObject("_source").toString(), EsProduct.class);
		return esproduct;
	}
	
	
	
	
	
	
	
	
	
	public static void main(String[] args) throws Exception {
	}
	

}
